package com.lekang.le.service.impl;

import java.util.Date;
import java.util.List;

import com.lekang.common.core.domain.entity.SysUser;
import com.lekang.common.utils.SecurityUtils;
import com.lekang.domain.NursingLevelYyh;
import com.lekang.le.domain.*;
import com.lekang.le.mapper.*;
import com.lekang.mapper.NursingLevelYyhMapper;
import com.lekang.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lekang.le.service.IElderlySzhService;

/**
 * 老人管理Service业务层处理
 * 
 * @author szh
 * @date 2025-10-13
 */
@Service
public class ElderlySzhServiceImpl implements IElderlySzhService 
{
    @Autowired
    private ElderlySzhMapper elderlySzhMapper;

    /**
     * 获取老人选择弹窗数据
     */
    @Override
    public List<ElderlySelectDTO> getElderlySelectList() {
        return elderlySzhMapper.selectElderlyWithAllInfo();
    }
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private NursingLevelYyhMapper nursingLevelYyhMapper;
    @Autowired
    private BedMapper bedMapper;
    @Autowired
    private ContractMapper contractMapper;
    @Autowired
    private CheckInManagementMapper checkInManagementMapper;
    @Autowired
    private CaregiverBedBindingZcqMapper caregiverBedBindingZcqMapper;

    /**
     * 查询老人管理
     * 
     * @param id 老人管理主键
     * @return 老人管理
     */
    @Override
    public ElderlySzh selectElderlySzhById(Long id)
    {
        return elderlySzhMapper.selectElderlySzhById(id);
    }
    // 新增方法实现：根据老人ID查询
    @Override
    public ElderlySelectDTO getElderlyByElderlyId(Long elderlyId) {
        return elderlySzhMapper.selectElderlyByElderlyId(elderlyId);
    }
    /**
     * 查询老人管理列表
     * 
     * @param elderlySzh 老人管理
     * @return 老人管理
     */
    @Override
    public List<ElderlySzh> selectElderlySzhList(ElderlySzh elderlySzh)
    {
        return elderlySzhMapper.selectElderlySzhList(elderlySzh);
    }

    @Override
    public ElderlySzh selectElderlySzhAllById(Long id) {
        ElderlySzh elderlySzh = elderlySzhMapper.selectElderlySzhById(id);
        //查询床位信息
        Bed bed = bedMapper.selectBedById(elderlySzh.getBedId());
        //查询护理员信息
        List<Long> longs = caregiverBedBindingZcqMapper.selectByBedId(bed.getId());
        SysUser sysUsers[] = new SysUser[longs.size()];
        for(int i = 0 ; i < longs.size();i++ ){
            SysUser sysUser = sysUserMapper.selectUserById(longs.get(i));
            sysUsers[i] = sysUser;
        }
        //查询合同
        Contract contract = new Contract();
        contract.setElderlyId(id);
        List<Contract> contracts = contractMapper.selectContractList(contract);
        Contract contract1 = contracts.get(0);
        //查询入住管理表信息
        CheckInManagement checkInManagement  = new CheckInManagement();
        checkInManagement.setElderlyId(id);
        List<CheckInManagement> checkInManagements = checkInManagementMapper.selectCheckInManagementList(checkInManagement);
        CheckInManagement checkInManagement1 = checkInManagements.get(0);
        //查询护理等级信息
        NursingLevelYyh nursingLevelYyh = nursingLevelYyhMapper.selectNursingLevelById(checkInManagement1.getNursingLevelId());
        elderlySzh.setBed(bed);
        elderlySzh.setContract(contract1);
        elderlySzh.setNursingLevelYyh(nursingLevelYyh);
        elderlySzh.setCheckInManagement(checkInManagement1);
        elderlySzh.setSysUser(sysUsers);
//        System.out.println(elderlySzh);
        return elderlySzh;
    }


    /**
     * 新增老人管理
     * 
     * @param elderlySzh 老人管理
     * @return 结果
     */
    @Override
    public int insertElderlySzh(ElderlySzh elderlySzh)
    {
        return elderlySzhMapper.insertElderlySzh(elderlySzh);
    }

    /**
     * 修改老人管理
     * 
     * @param elderlySzh 老人管理
     * @return 结果
     */
    @Override
    public int updateElderlySzh(ElderlySzh elderlySzh)
    {
        return elderlySzhMapper.updateElderlySzh(elderlySzh);
    }

    /**
     * 批量删除老人管理
     * 
     * @param ids 需要删除的老人管理主键
     * @return 结果
     */
    @Override
    public int deleteElderlySzhByIds(Long[] ids)
    {
        return elderlySzhMapper.deleteElderlySzhByIds(ids);
    }

    /**
     * 删除老人管理信息
     * 
     * @param id 老人管理主键
     * @return 结果
     */
    @Override
    public int deleteElderlySzhById(Long id)
    {
        return elderlySzhMapper.deleteElderlySzhById(id);
    }

    @Override
    public List<ElderlyBedVO> selectElderlyByWxUserId(Long wxUserId) {
        List<ElderlyBedVO> elderlyBedVOS = elderlySzhMapper.selectElderlyByWxUserId(wxUserId);
        return elderlyBedVOS;
    }
}
